package com.dt.global.web.action.Newscentrality;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.codehaus.jackson.map.ObjectMapper;
import org.hibernate.HibernateException;
import org.springframework.context.annotation.Scope;

import com.dt.components.exstruts.action.BaseActionSupport;
import com.dt.global.entitys.bo.TWebCoreArticle;
import com.dt.global.entitys.bo.TWebCoreChannel;
import com.dt.global.utils.Proerties;
import com.dt.global.web.mar.Newscentrality.NewscentralityMar;
/**
 * 首页新闻查询的Action
 * @author MWG
 *
 */
@Scope("prototype")
public class NewscentralityAction  extends BaseActionSupport {
	private static final long serialVersionUID = 1L;
	//json解析对象
	public ObjectMapper mapper=new ObjectMapper();
	@Resource
	public NewscentralityMar newMar;

	private TWebCoreArticle twebCoreArticle;
	/**
	 * @author BYu
	 * @explain 首页页头
	 * @return success
	 * @throws HibernateException
	 */
	public String header() throws HibernateException {
		try {
			return SUCCESS;
		} catch (Exception e) {
			return "error";
		}
	}

	/**
	 * @author BYu
	 * @explain 首页页脚
	 * @return success
	 * @throws HibernateException
	 */
	public String footer() throws HibernateException {
		try {
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}


	/*
	 * 加载首页新闻闻
	 * 
	 */

	public String addNews(){
		HttpServletRequest req = 
			ServletActionContext.getRequest();
		HttpServletResponse resp = 
			ServletActionContext.getResponse();
		resp.setContentType("text/html;utf-8");
		resp.setCharacterEncoding("utf-8");
		try {
			Proerties popUtil = new Proerties();
			String url = popUtil.getValue("wjrdht");
			String channelId = req.getParameter("channelId");
			List<Map<String,String>> list = new ArrayList<Map<String,String>>();
			List<TWebCoreArticle> listArticle = newMar.getTWebCoreArticle(channelId);
			//声明Map对象
			Map<String,String> map = null;
			if(channelId.equals("channelId201309251495124079477760")){
				for (TWebCoreArticle core : listArticle) {
					map = new HashMap<String, String>();
					if(core.getTitle().length()>14){
						map.put("title", core.getTitle().substring(0, 14)+"...");
					}else{
						map.put("title", core.getTitle());
					}
					map.put("articleId", core.getArticleId());
					list.add(map);
				}
			}else if(channelId.equals("channelId201309251495124433224704")){
				for (TWebCoreArticle core : listArticle) {
					map = new HashMap<String, String>();
					if(core.getTitle().length()>14){
						map.put("title", core.getTitle().substring(0, 14)+"...");
					}else{
						map.put("title", core.getTitle());
					}
					map.put("time", core.getReleaseSysDate().substring(0,10));
					map.put("articleId", core.getArticleId());
					list.add(map);
				}
			}else if(channelId.equals("channelId201309271498002016551936")){
				for (TWebCoreArticle core : listArticle) {
					map = new HashMap<String, String>();
					map.put("articleId", core.getArticleId());
					if(core.getTitle().length()>10){
						map.put("title", core.getTitle().substring(0, 10)+"...");
					}else{
						map.put("title", core.getTitle());
					}
					map.put("allTitle", core.getTitle());
					if(core.getTitleImg()!=null&&core.getTitleImg().length()>0){
						map.put("titleImg",url+"/uploadfile/"+core.getTitleImg());
					}else{
						map.put("titleImg","");	
					}

					list.add(map);
				}
			}else if(channelId.equals("channelId201309301502084969792512")){
				for (TWebCoreArticle core : listArticle) {
					map = new HashMap<String, String>();
					if(core.getTitle().length()>14){
						map.put("title", core.getTitle().substring(0, 14)+"...");
					}else{
						map.put("title", core.getTitle());
					}
					map.put("time", core.getReleaseSysDate().substring(0,10));
					map.put("articleId", core.getArticleId());
					list.add(map);
				}
			}else{
				for (TWebCoreArticle core : listArticle) {
					map = new HashMap<String, String>();
					if(core.getTitle().length()>18){
						map.put("title", core.getTitle().substring(0, 18)+"...");
					}else{
						map.put("title", core.getTitle());
					}
					map.put("time", core.getReleaseSysDate().substring(0,10));
					map.put("articleId", core.getArticleId());
					list.add(map);
				}
			}

			String json = mapper.writeValueAsString(list);
			PrintWriter out  =resp.getWriter();
			out.print(json);
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	/**
	 * 查询栏目
	 */
	public String getTWebCoreChannel(){
		HttpServletResponse resp = 
			ServletActionContext.getResponse();
		resp.setContentType("text/html;utf-8");
		resp.setCharacterEncoding("utf-8");
		try {
			List<Map<String,String>> list = new ArrayList<Map<String,String>>();
			//声明Map对象
			Map<String,String> map = null;
			//分组查询栏目
			Proerties popUtil = new Proerties();
			String url = popUtil.getValue("wjrdxt");
			String pid=getRequest().getParameter("pid");
			if(pid==null){
				pid="00";
			}
			List<String> listobject=newMar.getTWebCoreChannelGroupByNid(pid);
			if(listobject!=null&&listobject.size()>0){
				for(int i=0;i<listobject.size();i++){
					map = new HashMap<String, String>();
					if(listobject.get(i)!=null&&!listobject.get(i).toString().equals("")){
						//根据pid查询数据
						List<TWebCoreChannel> listTweb=newMar.getTWebCoreChannelPid(listobject.get(i).toString());
						if(listTweb!=null&&listTweb.size()>0){
							map.put("name", listTweb.get(0).getParent());
							map.put("channelId", listTweb.get(0).getChannelId());
							if(listTweb.get(0).getParent().equals("代表风采")){
								map.put("dizhi","article/getdaibiaofcPageQuery.do?channelId=");
								map.put("qita","");
							}else if(listTweb.get(0).getParent().equals("基层专栏")){
								map.put("qita","<a href='"+url+"' target='_blank'>代表履职</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;");
								map.put("dizhi","article/articlejczlPageQuery.do?channelId=");
							}else{
								map.put("qita","");
								map.put("dizhi","article/articlePageQuery.do?channelId=");
							}
						}else{
							//根据nid进行查询
							List<TWebCoreChannel> listTwebnid=newMar.getTWebCoreChannelNid(listobject.get(i).toString());
							map.put("name", listTwebnid.get(0).getName());
							map.put("channelId", listTwebnid.get(0).getChannelId());
							if(listTwebnid.get(0).getName().equals("代表风采")){
								map.put("qita","");
								map.put("dizhi","article/getdaibiaofcPageQuery.do?channelId=");
							}else if(listTwebnid.get(0).getName().equals("基层专栏")){
								map.put("qita","<a href='"+url+"' target='_blank'>代表履职</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;");
								map.put("dizhi","article/articlejczlPageQuery.do?channelId=");
								break;
							}else{
								map.put("qita","");
								map.put("dizhi","article/articlePageQuery.do?channelId=");
							}
						}
					}
					list.add(map);
				}
			}

			String json = mapper.writeValueAsString(list);
			PrintWriter out  =resp.getWriter();
			out.print(json);
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	/**
	 * 镇街道查询栏目
	 */
	public String getTWebCoreChannelZhengjd(){
		HttpServletResponse resp = 
			ServletActionContext.getResponse();
		resp.setContentType("text/html;utf-8");
		resp.setCharacterEncoding("utf-8");
		try {
			List<Map<String,String>> list = new ArrayList<Map<String,String>>();
			//声明Map对象
			Map<String,String> map = null;
			//分组查询栏目
			Proerties popUtil = new Proerties();
			String url = popUtil.getValue("wjrdxt");
			String pid=getRequest().getParameter("pid");
			if(pid==null){
				pid="00";
			}
			List<String> listobject=newMar.getTWebCoreChannelGroupByNid(pid);
			if(listobject!=null&&listobject.size()>0){
				for(int i=0;i<listobject.size();i++){
					map = new HashMap<String, String>();
					if(listobject.get(i)!=null&&!listobject.get(i).toString().equals("")){
						//根据pid查询数据
						List<TWebCoreChannel> listTweb=newMar.getTWebCoreChannelPid(listobject.get(i).toString());
						if(listTweb!=null&&listTweb.size()>0){
							if(listTweb.get(0).getParent().substring(listTweb.get(0).getParent().length()-2).equals("概况")){
								map.put("name", listTweb.get(0).getParent());
							}else{
								if(listTweb.get(0).getParent().length()>5){
								map.put("name", listTweb.get(0).getParent().substring(listTweb.get(0).getParent().length()-4));	
								}else{
									map.put("name", listTweb.get(0).getParent());
								}
							}
							map.put("pid", listTweb.get(0).getPid().substring(0,2));
							map.put("channelId", listTweb.get(0).getChannelId());
							if(map.get("name").equals("代表风采")){
								map.put("dizhi","zhengymquery/getdaibiaofcPageQuery.do?channelId=");
								map.put("qita","");
							}else{
								map.put("qita","");
								map.put("dizhi","zhengymquery/articlezjdPageQuery.do?channelId=");
							}
						}else{
							//根据nid进行查询
							List<TWebCoreChannel> listTwebnid=newMar.getTWebCoreChannelNid(listobject.get(i).toString());
							map.put("name", listTwebnid.get(0).getName());
							map.put("channelId", listTwebnid.get(0).getChannelId());
							if(listTwebnid.get(0).getName().equals("代表风采")||map.get("name").equals("小组活动")){
								map.put("qita","");
								map.put("dizhi","zhengymquery/getdaibiaofcPageQuery.do?channelId=");
							}else{
								map.put("qita","");
								map.put("dizhi","zhengymquery/articlezjdPageQuery.do?channelId=");
							}
						}
					}
					list.add(map);
				}
			}
			String json = mapper.writeValueAsString(list);
			PrintWriter out  =resp.getWriter();
			out.print(json);
			out.flush();
			out.close();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
}
